package br.unb.graphics.help;

import br.unb.utils.Path;
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class OpenBrowser {

    private static boolean firstRun = true;
    private static boolean browserSupport = false;

    public void OpenHelp(String address) {
        OpenBrowser browser = new OpenBrowser();
        URL url = Path.getResourceUrlByAddress("help/" + address);
        browser.OpenBrowser(url);
    }

    public void OpenBrowser(URL url) {
        verifyBrowserSupport();
        try {
            URI uri = url.toURI();
            Desktop.getDesktop().browse(uri);
        } catch (URISyntaxException ex) {
            ex.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private boolean verifyBrowserSupport() {
        if (firstRun == true) {
            firstRun = false;
            if (!Desktop.isDesktopSupported()) {
                browserSupport = false;
                throw new IllegalStateException("Desktop resources not supported!");
            } else if (!Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
                browserSupport = false;
                throw new IllegalStateException("No default browser set!");
            } else {
                browserSupport = true;
            }
        }
        return browserSupport;
    }
}  
